<template>
  <div class="auth-container">
    <div class="auth-card">
      <h2 class="auth-title">
        <span>{{ pageTitle }}</span>
        <div class="title-decorator"></div>
      </h2>

      <form @submit.prevent="login" class="auth-form">
        <div class="form-group">
          <!-- 角色选择 -->
          <select v-model="data.role" id="role" class="custom-select">
            <option value="student">学生</option>
            <option value="company">企业</option>
          </select>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <i class="el-icon-user-solid icon"></i>
            <input
              type="text"
              v-model="data.username"
              id="username"
              placeholder="账号"
              class="custom-input"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <i class="el-icon-lock icon"></i>
            <input
              type="password"
              v-model="data.password"
              id="password"
              placeholder="密码"
              class="custom-input"
              required
            />
          </div>
        </div>

        <div class="button-group">
          <button type="submit" class="primary-button">登录</button>
          <button type="button" @click="goToRegister" class="secondary-button">
            注册
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import { reactive } from 'vue';
import { useRouter } from 'vue-router';

// 引入封装好的 axios 实例
import { service as request } from '@/utils/request';

// 引入 JWT 工具函数
import { setToken, parseJwt } from '@/utils/auth';

export default {
  name: 'Login',
  setup() {
    const router = useRouter();

    const data = reactive({
      role: 'student', // 默认角色
      username: '',
      password: '',
    });

    // 登录方法
  const login = () => {
  request.post('/api/login', data)
    .then(res => {
      if (res.code === 200) {
        const token = res.token || null;  // 从顶层获取 token
        if (token) {
          setToken(token);
          console.log('成功保存 Token:', token);
        } else {
          console.warn('⚠️ Token 不存在于响应中');
        }

        if (res.businessStatus === 0) {  // 从顶层获取 businessStatus
          ElMessage.success('登录成功');
          const userInfo = parseJwt();
          if (userInfo && userInfo.role) {
            if (userInfo.role === 'student') {
              router.push('/student/home');
            } else if (userInfo.role === 'company') {
              router.push('/company/home');
            }
          }
        } else if (res.businessStatus === 1) {
          ElMessage.warning('您的信息未完善，请补全资料');
          if (data.role === 'student') {
            router.push('/student/user');
          } else if (data.role === 'company') {
            router.push('/company/user');
          }
        }
      } else {
        ElMessage.error(res.msg || '登录失败');
      }
    })
    .catch(error => {
      ElMessage.error('登录失败，请检查网络或账号密码是否正确');
      console.error(error);
    });
};


    // 跳转注册页
    const goToRegister = () => {
      router.push('/register');
    };

    return {
      data,
      login,
      goToRegister,
      pageTitle: '登录',
    };
  },
};
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  display: flex;
  align-items: center;
  justify-content: flex-end; /* 右对齐 */
  padding: 20px;
  box-sizing: border-box;
}

.auth-card {
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  padding: 40px;
  animation: fadeIn 0.5s ease-out;
  margin-left: auto;
  margin-right: 20px;
}

.auth-title {
  font-family: 'Poppins', sans-serif;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 20px;
  position: relative;
  text-align: center;
}

.title-decorator {
  width: 60px;
  height: 4px;
  background: #3498db;
  margin: 10px auto 0;
  border-radius: 2px;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group label {
  font-size: 14px;
  color: #34495e;
  margin-bottom: 6px;
  font-weight: 500;
}

.custom-select,
.custom-input {
  width: 90%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  height: 48px;
  transition: all 0.3s ease;
}

.custom-select:focus,
.custom-input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.input-wrapper {
  position: relative;
}

.icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  font-size: 18px;
}

.button-group {
  display: flex;
  gap: 12px;
  flex-direction: column;
}

.primary-button {
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.primary-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.secondary-button {
  background: #ecf0f1;
  color: #34495e;
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.secondary-button:hover {
  background: #dcedf5;
  color: #2980b9;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 500px) {
  .auth-card {
    padding: 30px 20px;
  }

  .custom-input,
  .custom-select {
    font-size: 13px;
    padding: 10px;
  }
}
</style>
